// pages/exchangeGold/exchangeGold.js
const http = require('../../utils/http.js');
const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		select: 0,
		exchange: [{
			"goldNum": 12,
			"paymentPrice": 100,
			"selectStatus": true
		}, {
			"goldNum": 12,
			"paymentPrice": 50,
			"selectStatus": false
		}, {
			"goldNum": 12,
			"paymentPrice": 20,
			"selectStatus": false
		}, {
			"goldNum": 12,
			"paymentPrice": 6,
			"selectStatus": false
		}]
	},
	exchange(e) {
		let id = e.currentTarget.id;
		let up = "exchange[" + id + "].selectStatus";
		this.setData({
			select: id,
			[up]: true
		})
	},
	conformExchange() {
    var uid = app.globalData.uid;
    var select = this.data.select;
    var bl_jine = this.data.exchange[select].paymentPrice;
   // var bl_jine = 0.01;
    var bl_jinb = bl_jine * this.data.proportion
    console.log(bl_jine);

    if(this.data.cj=='qq'){

      http.fetchApi('qqPay/api/zf', { jes: bl_jine * 100, lx: 4, uid, order_id:0}).then(payParmms => {
        console.log(`qqPay/api/zf参数：jes: ${bl_jine} * 100, lx: 4, ${uid}`)
        qq.requestPayment({
          package: payParmms.data.packagess,
          success:()=>{
            http.fetchApi('procedure_t', {
              p: `R2008002|${uid}|${bl_jine}|${bl_jinb}|5008`
            }).then(res => {
              console.log(`R2008002参数：R2008002|${uid}|${bl_jine}|${bl_jinb}|5008`)
              if (res.data.code == 1) {
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000,
                  success: () => {
                    setTimeout(() => {
                      wx.switchTab({
                        url: '../PersonalCenter/PersonalCenter',
                      })
                    }, 500)
                  }
                })
              } else {
                wx.showToast({
                  title: '失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          },
          fail:err=>{
            http.showToast('支付失败!' + '', '', 'none')
          }
        })
        
        
      })
      
    }else{
      http.fetchApi('api/zf', { jes: bl_jine * 100, lx: 1, uid, order_id:0}).then(payParmms => {
        console.log(payParmms.data)
        var t = payParmms.data.timestampss,
          n = payParmms.data.nonceStrss,
          p = payParmms.data.packagess,
          s = payParmms.data.signTypess,
          ps = payParmms.data.paySignss;
        http.payment(t, n, p, s, ps).then(() => {
          http.fetchApi('procedure_t', {
            p: `R2008002|${uid}|${bl_jine}|${bl_jinb}|5001`
          }).then(res => {
            if (res.data.code == 1) {
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000,
                success: () => {
                  setTimeout(() => {
                    wx.switchTab({
                      url: '../PersonalCenter/PersonalCenter',
                    })
                  }, 500)
                }
              })
            } else {
              wx.showToast({
                title: '失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }).catch(err => { http.showToast('支付失败!' + '', '', 'none') })
      })
    }

   
   
	},

  //金币现金兑换比例
  getExchange() {
    http.fetchApi('procedure_t', { p: 'R2008003'}).then(res=>{
      if(res.data.code==1){
        var result = res.data.results[0];
        var proportion =result.name; //换算比例
       
        this.setData({
          proportion
        })
      }
    })
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
 
	onLoad: function(options) {
	
    var goldTotal = options.goldTotal;
    this.setData({
      goldTotal,
      cj: app.globalData.cj || ''
    })
    this.getExchange();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
